<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * create Registerpage
 * get: register.tpl.php
 * used from register.php
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */

$captcha = new Catcha;
$captcha->setImageSize(CAPTCHA_WIDTH, CAPTCHA_HEIGHT);
$captcha->setImageColorBackground(CAPTCHA_BACKGOUND);

db::getInstance()->where('page_name', 'new-registration');
$result = db::getInstance()->getOne('page');

$refPage->assign('header', $result['page_header']);
$texte = array();
$texte = explode('{BOTTOM}', $result['page_text']);

$refPage->assign('textTop', $texte[0]);
if (count($texte) == 2)
	$refPage->assign('textBottom', $texte[1]);

$buffer = '';

if (\COMPANY > 0)
{
	if (isset($_POST['companyx']))
		$buffer = $_POST['companyx'];

	$refPage->assign('company',
		'<label for=companyx>' . _('Company') . registerField(COMPANY) . ': </label>');
	$refPage->assign('companyx',
		'<input type="text" name="companyx" id="companyx" value="' . $buffer . '" size="35" />');
}
$buffer = '';

if (FIRSTNAME > 0)
{
	if (isset($_POST['firstnamex']))
		$buffer = $_POST['firstnamex'];

	$refPage->assign('firstname',
		'<label for="firstnamex">' . _('Firstname') . registerField(FIRSTNAME) . ': </label>');
	$refPage->assign('firstnamex',
		'<input type="text" name="firstnamex" id="firstnamex" value="' . $buffer . '" size="35" />');
}
$buffer = '';

if (\LASTNAME > 0)
{
	if (isset($_POST['lastnamex']))
		$buffer = $_POST['lastnamex'];

	$refPage->assign('lastname',
		'<label for = "lastnamex">' . _('Lastname') . registerField(LASTNAME) . ': </label>');
	$refPage->assign('lastnamex',
		'<input type="text" name="lastnamex" id="lastnamex" value="' . $buffer . '" size="35" />');
}
$buffer = '';

if (\STREET > 0)
{
	if (isset($_POST['streetx']))
		$buffer = $_POST['streetx'];

	$refPage->assign('street',
		'<label for="streetx">' . _('Street and Number') . registerField(STREET) . ': </label>');
	$refPage->assign('streetx',
		'<input type="text" name="streetx" id="streetx" value="' . $buffer . '" size="35" />');
}
$buffer = '';

if (ZIP_CODE > 0)
{
	if (isset($_POST['zipx']))
		$buffer = $_POST['zipx'];

	$refPage->assign('zip',
		'<label for="zipx">' . _('Zipcode') . registerField(ZIP_CODE) . ': </label>');
	$refPage->assign('zipx',
		'<input type="text" name="zipx" id="zipx" value="' . $buffer . '" size="10" />');
}
$buffer = '';

if (\CITY > 0)
{
	if (isset($_POST['cityx']))
		$buffer = $_POST['cityx'];

	$refPage->assign('city',
		'<label for="cityx">' . _('City') . registerField(CITY) . ': </label>');
	$refPage->assign('cityx',
		'<input type="text" name="cityx" id="cityx" value="' . $buffer . '" size="35" />');
}
$buffer = '';

if (\COUNTRY > 0)
{
	if (isset($_POST['countryx']))
		$buffer = $_POST['countryx'];

	$refPage->assign('country',
		'<label for="countryx">' . _('Country') . registerField(CITY) . ': </label>');
	db::getInstance()->where('config_key', 'country');
	db::getInstance()->orderBy("config_text", "ASC");
	$customerCountry = db::getInstance()->get('config');

	$countryx = '';
	foreach ($customerCountry as $group)
	{
		$countryx .= '<option value="' . $group['config_text'] . '" ';
		if ($group['config_text'] == $buffer)
			$countryx .= 'selected=\'selected\'';

		$countryx .= '>' . $group['config_text'] . '</option>';
	}
	$refPage->assign('countryx', $countryx);
}

if (\STATE > 0)
{
	$refPage->assign('state',
		'<label for="statex">' . _('State') . registerField(CITY) . ': </label>');
	$refPage->assign('statex',
		'<input type="text" name="statex" id="cityx" value="' . DEFAULT_STATE .
		'" disabled="disabled" size="35" />');
}
$buffer = '';

if (\PHONE > 0)
{
	if (isset($_POST['phonex']))
		$buffer = $_POST['phonex'];

	$refPage->assign('phone',
		'<label for="phonex">' . _('Phone') . registerField(PHONE) . ': </label>');
	$refPage->assign('phonex',
		'<input type="text" name="phonex" id="phonex" value="' . $buffer . '" size="35" />');
}
$buffer = '';

if (\FAX > 0)
{
	if (isset($_POST['faxx']))
		$buffer = $_POST['faxx'];

	$refPage->assign('fax',
		'<label for="faxx">' . _('Fax') . registerField(FAX) . ': </label>');
	$refPage->assign('faxx',
		'<input type="text" name="faxx" id="faxx" value="' . $buffer . '" size="35" />');
}
$buffer = '';

if (MOBILE > 0)
{
	if (isset($_POST['handyx']))
		$buffer = $_POST['handyx'];

	$refPage->assign('handy',
		'<label for="handyx">' . _('Mobil') . registerField(MOBILE) . ': </label>');
	$refPage->assign('handyx',
		'<input type="text" name="handyx" id="handyx" value="' . $buffer . '" size="35" />');
}
$buffer = '';

if (\EMAIL > 0)
{
	if (isset($_POST['emailx']))
		$buffer = $_POST['emailx'];

	$refPage->assign('email',
		'<label for="emailx">' . _('E-Mail') . registerField(EMAIL) . ': </label>');
	$refPage->assign('emailx',
		'<input type="text" name="emailx" id="emailx" value="' . $buffer . '" size="35" />'
	);
}
$buffer = '';
if (isset($_POST['namex']))
	$buffer = $_POST['namex'];

$refPage->assign('name', '<label for="namex">' . _('Username') . '*' . ': </label>');
$refPage->assign('namex',
	'<input type="text" name="namex" id="namex" value="' . $buffer . '" size="35" />');
$buffer = '';

$refPage->assign('password',
	'<label for="passwordx">' . _('Password') . '*' . ': </label>');
$refPage->assign('passwordx',
	'<input type="password" name="passwordx" id="passwordx" value="" size="50" autocomplete="off"/>');

$refPage->assign('btnRegister',
	'<input type="submit" name="submit" id="submit" value="' .
	_('Register') . '" />' .
	'<input type="hidden" name="func" value="register"/>');

if (CAPTCHA_ON)
{
	//$refPage->assign('action', 'module/function/captcha/verifier.php? ' . SID);
	$refPage->assign('action', 'index.php?page=register&name=captcha');
	
	$image_data = $captcha->getImage();
	$file = fopen('tmp/captcha.jpg', 'w');
	fwrite($file, $image_data);
	fclose($file);
	$_SESSION['captcha'] = serialize($captcha);
	$refPage->assign('captcha', '<img src="tmp/captcha.jpg" alt="Captcha" titel="Captcha"/>');
}
else
{
	$refPage->assign('action', 'index.php?page=register&name=newregister');
	//$refPage->assign('captcha', '');
}

$refPage->addtpl('content', '/register.tpl.php');

/**
 * Check if Field are required
 *
 * @param  integer	Value from config.php
 * @return string		'*' if field required
 */
function registerField($parameter)
{
	if ($parameter === 2)
	{
		return '*';
	}
	else
	{
		return ' ';
	}
}

?>
